package tt.com.serviceprojectmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tt.com.common.model.YxCommonServeProviders;
import tt.com.common.model.YxCommonServeType;
import tt.com.serviceprojectmanagement.mapper.YxCommonServeProvidersMapper;
import tt.com.serviceprojectmanagement.mapper.YxCommonServeTypeMapper;
import tt.com.serviceprojectmanagement.service.TypeDateListService;

import java.util.Date;
import java.util.List;

import static java.sql.JDBCType.NULL;

/**
 * @author BEAN
 * @version 2020
 * @date 2022/8/31 10:40
 */
@Service
public class TypeDateListServiceImpl extends ServiceImpl<YxCommonServeTypeMapper, YxCommonServeType> implements TypeDateListService {

    @Autowired
    private YxCommonServeTypeMapper yxCommonServeTypeMapper;

    @Autowired
    private YxCommonServeProvidersMapper providersMapper;


    @Override
    public Boolean delTypeDate(String id, String delUserId) {
        // 判断当前分类是否已经删除
        YxCommonServeType yxCommonServeType = yxCommonServeTypeMapper.selectById(id);

        if (yxCommonServeType.getDataStatus() == 0) {
            return false;
        }

        yxCommonServeType.setDeleteUserId(delUserId);
        yxCommonServeType.setDataStatus(0);
        yxCommonServeType.setDeleteTime(new Date());

//        int i = yxCommonServeTypeMapper.deleteById(id);  逻辑删除暂时还未能实现设置删除时间 不推荐使用
        return yxCommonServeTypeMapper.updateById(yxCommonServeType) > 0;
    }

    @Override
    public List<YxCommonServeType> searchAllType() {
        QueryWrapper<YxCommonServeType> wrapper = new QueryWrapper<>();
//        wrapper.isNull("parent_id");
        wrapper.isNull("parent_id");
        List<YxCommonServeType> yxCommonServeTypes = yxCommonServeTypeMapper.selectList(wrapper);
        return yxCommonServeTypes;
    }

    @Override
    public Boolean updateTypeName(String id, String newName) {
        YxCommonServeType yxCommonServeType = yxCommonServeTypeMapper.selectById(id);
        if (yxCommonServeType.getDataStatus() == 0) {
            // 数据已删除 不可修改
            return false;
        }

        yxCommonServeType.setName(newName);
        return yxCommonServeTypeMapper.updateById(yxCommonServeType) > 0;
    }

    @Override
    public List<YxCommonServeProviders> searchAll(String serveTypeId) {
        QueryWrapper<YxCommonServeProviders> wrapper = new QueryWrapper<>();
        wrapper.eq("serve_type_id", serveTypeId);
        List<YxCommonServeProviders> list = providersMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Integer insertNewType(YxCommonServeType serveType) {
        //判断要新增的分类是否存在
        YxCommonServeType yxCommonServeType = yxCommonServeTypeMapper.selectById(serveType.getId());
        if (yxCommonServeType != null) {
            return 2;
        }
        int insert = yxCommonServeTypeMapper.insert(serveType);
        return insert;
    }

    @Override
    public Integer insertNewServeProviders(YxCommonServeProviders serveProviders) {
        int insert = providersMapper.insert(serveProviders);
        return null;
    }
}
